<?php
/**
 * Модель order
 * 
 * Класс модели
 * @author Anton Zavidov
 * @package myLiveShop
 */

/**
 * Модель order
 *
 */
class ModelOrder extends Model {
	
	const TABLE				= 'order';
	const F_ORDER_ID  	= 'order_id';
	const F_SHOP_ID  	= 'shop_id';
	const F_USER_ID  	= 'user_id';
	const F_CREATED  	= 'created';
	const F_DESCRIPTION  	= 'description';
	const F_STATUS  	= 'status';
	const F_STATUS_COMMENT  	= 'status_comment';
	const F_COUPON_ID  	= 'coupon_id';
			
	public function __construct($className=null){
		parent::__construct(__CLASS__);
		$this->name='order';
		$this->id_field=array(self::F_ORDER_ID);
		$this->fields=array(
		self::TABLE.DOT.self::F_ORDER_ID=> new Field($this,self::F_ORDER_ID,self::TABLE,array(
				'auto'=>true,
				'important'=>true,
				'lenght'=>11)),
		self::TABLE.DOT.self::F_SHOP_ID=> new Field($this,self::F_SHOP_ID,self::TABLE,array(
				'important'=>true,
				'lenght'=>9,
				'join_table'=>'shop')),
		self::TABLE.DOT.self::F_USER_ID=> new Field($this,self::F_USER_ID,self::TABLE,array(
				'important'=>true,
				'lenght'=>11,
				'join_table'=>'user')),
		self::TABLE.DOT.self::F_CREATED=> new Field($this,self::F_CREATED,self::TABLE,array(
				'important'=>true,
				'auto'=>true,
				'type'=>Field::TYPE_DATETIME)),
		self::TABLE.DOT.self::F_DESCRIPTION=> new Field($this,self::F_DESCRIPTION,self::TABLE,array(
				'type'=>Field::TYPE_TEXT)),
		self::TABLE.DOT.self::F_STATUS=> new Field($this,self::F_STATUS,self::TABLE,array(
				'lenght'=>1)),
		self::TABLE.DOT.self::F_STATUS_COMMENT=> new Field($this,self::F_STATUS_COMMENT,self::TABLE,array(
				'lenght'=>255,
				'type'=>Field::TYPE_STRING)),
		self::TABLE.DOT.self::F_COUPON_ID=> new Field($this,self::F_COUPON_ID,self::TABLE,array(
				'lenght'=>6,
				'join_table'=>'coupon'))		);
	}

	//получаем список продуктов по заказу
	public function orderList($order_id){
		import("model.product");
		import("model.image");
		$product_to_order = import("model.product_to_order",true);
		return $product_to_order->load(null,array(
				ModelProduct::F_PRODUCT_ID, 
				ModelProduct_to_order::F_PRODUCT_COUNT,
				ModelProduct_to_order::F_PARAMS,
				"(".ModelProduct_to_order::F_PRODUCT_COUNT." * ".ModelProduct::F_REAL_PRICE.") AS totallPrice",
				ModelProduct::TABLE.DOT.ModelProduct::F_TITLE,
				ModelProduct::TABLE.DOT.ModelProduct::F_REAL_PRICE,
				ModelImage::TABLE.DOT.ModelImage::F_FILENAME,
			),array(
			'where'=>ModelProduct_to_order::F_ORDER_ID .'='.$order_id,
			'join'=>array(
					array('table'=>ModelProduct::TABLE, 'key'=>ModelProduct::F_PRODUCT_ID, 'join_table'=>ModelProduct_to_order::TABLE, 'join_field'=>ModelProduct_to_order::F_PRODUCT_ID),
					array('table'=>ModelImage::TABLE, 'key'=>ModelImage::F_IMAGE_ID, 'join_table'=>ModelProduct::TABLE, 'join_field'=>ModelProduct::F_IMAGE_ID)
			)
		));
	}
}
